package com.supermap.dataservice.agrinfo.dao;

import com.supermap.dubbo.api.agrinfo.entity.Farming;
import com.supermap.dubbo.api.agrinfo.entity.FarmingType;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author:langhongshuai
 * @Description:农事种类数据访问层
 * @Date:18/1/8 上午11:36
 */
@Repository
@Mapper
public interface FarmingDao {


    /**
     * 保存农事信息
     * @param farming
     */
    @Insert("Insert t_farming(id,areacode,farmlandid,type,croptype,"
            + "longitude,latitude,recordtime,description,createtime,img_url,userid) "
            + "values(#{Farming.id},#{Farming.areacode},#{Farming.farmlandid},#{Farming.type},"
            + "#{Farming.croptype},#{Farming.longitude},#{Farming.latitude},#{Farming.recordtime}"
            + ",#{Farming.description},#{Farming.createtime},#{Farming.imgurl},#{Farming.userid})")
    public void saveFarming(@Param("Farming")Farming farming);

    /**
     * 根据条件获取农事情况
     * @param farming
     */
    @Select("SELECT\n" +
            "\tt1.*,t2.name as typename,t3.farmname,t4.`name` as cropname,t5.username\n" +
            "FROM\n" +
            "\tt_farming t1\n" +
            "LEFT JOIN t_farmingtype t2 ON t2.`code` = t1.type\n" +
            "left join t_farmland t3 on t3.id = t1.farmlandid\n" +
            "left join t_crop t4 on t4.`code` = t1.croptype\n" +
            "left JOIN t_user t5 on t5.id = t1.userid where " +
            "if(#{Farming.userid}=0,true,t1.userid=#{Farming.userid})" +
            "and if(isnull(#{Farming.areacode}),true,t1.areacode=#{Farming.areacode})" +
            "and if(#{Farming.farmlandid}=0,true,t1.farmlandid=#{Farming.farmlandid})" +
            "and if(#{Farming.id}=0,true,t1.id=#{Farming.id}) " +
            "and if(isnull(#{Farming.croptype}),true,t1.croptype=#{Farming.croptype})" +
            "order by t1.recordtime DESC")
    public List<Farming> getFarming(@Param("Farming")Farming farming);

    /**
     * 更新农事信息
     * @param farming
     */
    @Update("update t_farming set id=#{Farming.id},areacode=#{Farming.areacode}," +
            "farmlandid=#{Farming.farmlandid},type=#{Farming.type},croptype=#{Farming.croptype}," +
            "longitude=#{Farming.longitude},latitude=#{Farming.latitude},recordtime=#{Farming.recordtime}," +
            "description=#{Farming.description},img_url=#{Farming.imgurl},userid=#{Farming.userid}")
    public void updateFarming(@Param("Farming")Farming farming);


    /**
     * 通过农事id删除农事信息
     * @param id
     */
    @Delete("delete from t_farming where id = #{id}")
    public void deleteFarmingById(@Param("id")String id);

    /**
     * 根据等级获得所有的农事信息
     * @return
     */
    @Select("select * from t_farmingtype where level=#{level}")
    public List<FarmingType> getAllFarmingTypeByLevels(@Param("level") int level);

    /**
     * 获得所有的农事种类信息的等级信息
     * @return
     */
    @Select("select * from t_farmingtype")
    public List<FarmingType> getAllFarmingType();

    /**
     * 插入农事种类信息
     * @param farmingType
     */
    @Insert("insert t_farmingtype(id,code,name,description,level,parentid,img_ico) values("
            + "#{FarmingType.id},#{FarmingType.code},#{FarmingType.name},"
            + "#{FarmingType.description},#{FarmingType.level},#{FarmingType.parentid},#{FarmingType.img_ico})")
    public void saveFarmingType(@Param("FarmingType") FarmingType farmingType);

    /**
     * 根据id更新农事种类信息
     * @param farmingType
     */
    @Update("update t_farming set code=#{FarmingType.code},"
            + "name=#{FarmingType.name},description=#{FarmingType.description},level=#{FarmingType.level},"
            + "parentid=#{FarmingType.parentid},img_ico=#{FarmingType.img_ico} where id=#{FarmingType.id}")
    public void updateFarmingType(@Param("FarmingType") FarmingType farmingType);

    /**
     * 根据农事id删除农事信息
     * @param id
     */
    @Delete("delete from t_farmingtype where id=#{id} ")
    public void deleteFarmingType(@Param("id") String id);

}
